package cn.edu.zut.soft.student.service.student.impl;

import cn.edu.zut.soft.student.bean.PageBean;
import cn.edu.zut.soft.student.dao.ClassMapper;
import cn.edu.zut.soft.student.dao.SelectCourseMapper;
import cn.edu.zut.soft.student.dao.StudentMapper;
import cn.edu.zut.soft.student.model.Course;
import cn.edu.zut.soft.student.model.SelectCourse;
import cn.edu.zut.soft.student.model.SelectCourseKey;
import cn.edu.zut.soft.student.model.StudentSelectCourses;
import cn.edu.zut.soft.student.service.student.StudentSelectCoursesBo;
import cn.edu.zut.soft.student.service.student.vo.StudnetQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 19:23
 */

@Service
public class StudentSelectCoursesBoImpl implements StudentSelectCoursesBo {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private SelectCourseMapper scm;

    @Override
    public PageBean<List<StudentSelectCourses>> queryStudentSelectCourses(StudnetQuery studnetQuery) {
        List<StudentSelectCourses> selectcourses = studentMapper.selectcourses().stream().distinct().collect(Collectors.toList());
        if (studnetQuery.getStudentname() != null && studnetQuery.getStudentname() != "") {
            selectcourses = selectcourses.stream().filter(sc -> sc.getStudent().getStudentname().contains(studnetQuery.getStudentname())).collect(Collectors.toList());
        }
        if (studnetQuery.getStudentid() != null && studnetQuery.getStudentid() != "") {
            selectcourses = selectcourses.stream().filter(sc -> sc.getStudent().getStudentid().contains(studnetQuery.getStudentid())).collect(Collectors.toList());
        }
        long total = selectcourses.size();
        selectcourses = selectcourses.stream().skip(studnetQuery.getOffset()).limit(studnetQuery.getLimit()).collect(Collectors.toList());
        selectcourses.forEach(sc -> sc.getStudent().setStudentclassid(classMapper.selectByPrimaryKey(sc.getStudent().getStudentclassid()).getClassname()));
        return new PageBean<>(selectcourses, total);
    }

    @Override
    public List<Course> querySelectCourses(String studentid) {
        List<Course> courses = studentMapper.querySelectCourseByStudentID(studentid);
        courses = courses.stream().sorted(Comparator.comparing(Course::getCoursetype).reversed()).collect(Collectors.toList());
        return courses;
    }

    @Override
    public SelectCourse queryScore(String studentid, String courseid) {
        SelectCourseKey selectCourseKey = new SelectCourseKey();
        selectCourseKey.setSelectcoursestudentid(studentid);
        selectCourseKey.setSelectcourseid(courseid);
        return scm.selectByPrimaryKey(selectCourseKey);
    }

    @Override
    public int updateScore(SelectCourse selectCourse) {
        return scm.updateByPrimaryKey(selectCourse);
    }
}
